/**
 * 发送请求
 */
export default function request(api, method, data = {}) {
	// console.log(data, api);
	let header
	let HTTP_REQUEST_URL = 'http://127.0.0.1:8081'
	//let token = uni.getStorageSync(TOKEN_NAME) || null
	let CONTENT_TYPE = 'application/json' // 'application/x-www-form-urlencoded' 
	header = {
		'Content-Type': CONTENT_TYPE
	}

	return new Promise((reslove, reject) => {
		uni.showNavigationBarLoading()
		console.log("request")
		uni.request({
			url: HTTP_REQUEST_URL + api,
			method: method || 'GET',
			header: header,
			data,
			success: (res) => {
				// console.log(res, api);
				if (!res.data) {
					reject('请求失败');
					uni.hideNavigationBarLoading()
				} else {
					uni.hideNavigationBarLoading()
					if (res.data.code === '0') { //成功
						reslove(res.data);
					} else if (res.data.code !== '0') { // 错误
						//uni.removeStorageSync(TOKEN_NAME)
						uni.$emit('needLogin')
						reject(res.data);
						uni.reLaunch({
							url: '/pages/loginOnlyPhone/loginOnlyPhone'
						})
					} else {
						reject(res.data);
					}
				}
			},
			fail: (msg) => {
				uni.hideNavigationBarLoading()
				console.log(msg);
				reject('请求失败');
			}
		})
	});
}

['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
	request[method] = (api, data) => request(api, method, data)
});
